home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 045 (1988-02-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 045 (1988-02-15)(Ossowski, Stefan)(DE)(PD).adf / Iff2Pcs / Source / pzmain.c < prev    next >
C/C++ Source or Header  |  1988-01-20  |  7KB  |  225 lines

  1. #include "pz.h"
  2. #include "smalliff.h"
  3.  
  4. /* IFF2PCS, main() and various other routines...
  5. ** IFF2PCS (c)1987 Ali T. Ozer
  6. ** Nov 1987
  7. */
  8. struct Library *IntuitionBase, *GfxBase, *LayersBase;
  9.  
  10. PicInfoStruct      pic;
  11. struct Screen     *scr0;
  12. struct FileHandle *fp, *ParseArgs ();
  13.  
  14. int runningfromcli;
  15.  
  16. extern struct Menu pzmenu;
  17. int textcolor, bordercolor, normcolor, nonzerocolor;
  18. int puzpn, puzdepth;   /* These are the parameters for a piece */
  19. int picx, picy;        /* Where the picture is */
  20. int picw, pich;        /* The size of the picture */
  21.  
  22. struct BitMap picbm;  /* All plane pointers start as NULL */
  23.  
  24. /* Panic puts up a requester with a single "Sigh..." box. The string
  25. ** provided in "reason" is printed in the body of the  requester. 
  26. ** If user hits "Retry," then Panic returns. Else it exits.
  27. */
  28. void Panic (reason)
  29. UBYTE *reason;
  30. {
  31.   static struct IntuiText negtxt  = {0,1,COMPLEMENT,4,4,NULL,(UBYTE *)"Sigh...",NULL};
  32.   static struct IntuiText bodytxt = {0,1,COMPLEMENT,10,6,NULL,NULL,NULL};
  33.  
  34.   if (reason) {
  35.     if (runningfromcli == true) puts (reason);
  36.     else {
  37.       bodytxt.IText = reason;
  38.       WBenchToFront ();
  39.       if (AutoRequest (NULL, &bodytxt, NULL, &negtxt, 0L, 0L, 
  40.                              (long)(Max(Min(strlen(reason)*10+50,625),200)),
  41.                              54L) == TRUE) return;
  42.     }
  43.   };
  44.   CleanUp ();
  45.   exit (5);
  46. }
  47.  
  48.  
  49. CleanUp ()
  50. {
  51.   FreePzMenu ();  
  52.  
  53.   FreeBM (&picbm);
  54.  
  55.   if (pic.rawsource) FreeMem (pic.rawsource, pic.rawsourcesize);
  56.  
  57.   if (scr0) {
  58.     if (scr0->FirstWindow) {
  59.     /*if (scr0->FirstWindow->MenuStrip) ClearMenuStrip (scr0->FirstWindow);*/
  60.       CloseWindow (scr0->FirstWindow);  /* Assume only 1 */
  61.     };
  62.     CloseScreen  (scr0);
  63.   };
  64.  
  65.   if (LayersBase)        CloseLibrary (LayersBase);
  66.   if (GfxBase)           CloseLibrary (GfxBase);
  67.   if (IntuitionBase)     CloseLibrary (IntuitionBase);
  68. }
  69.  
  70. int Min ();
  71. int Max ();
  72.  
  73. struct TextAttr NormalFontDesc = {(STRPTR)"topaz.font", 8, 0, 0};
  74.  
  75. struct Screen *OpenPictureScreen (pic, borderpen, detailpen)
  76. PicInfoStruct *pic;
  77. {
  78.   struct NewScreen ns;
  79.   struct NewWindow nw;
  80.  
  81.   setmem (&ns, sizeof(ns), 0);
  82.   setmem (&nw, sizeof(nw), 0);
  83.  
  84.   ns.DefaultTitle  = (UBYTE *)PROGNAME;
  85.   ns.Font = &NormalFontDesc; 
  86.   ns.Width  = nw.Width  = 
  87.        ((struct GfxBase *)GfxBase)->NormalDisplayColumns;    /* pic->bmhd.w; */
  88.   ns.Height = nw.Height =
  89.        ((struct GfxBase *)GfxBase)->NormalDisplayRows * 2; /* pic->bmhd.h; */
  90.   ns.DetailPen = nw.DetailPen = detailpen; 
  91.   ns.BlockPen  = nw.BlockPen  = borderpen;  
  92.   ns.Depth  = pic->bmhd.nPlanes;  
  93.   ns.Type   = nw.Type   = CUSTOMSCREEN;
  94.   nw.Flags  = SMART_REFRESH | BORDERLESS | BACKDROP | RMBTRAP | ACTIVATE;
  95.   nw.IDCMPFlags = MOUSEBUTTONS | MOUSEMOVE | GADGETUP /*| MENUPICK*/;
  96.   ns.ViewModes  = HIRES | LACE;  /* pic->viewmodes; */
  97.   if (nw.Screen = OpenScreen (&ns)) {
  98.     if (OpenWindow (&nw)) return (nw.Screen);
  99.     else CloseScreen (nw.Screen);
  100.   } else {   /* Try with a smaller screen and see if we can open that. */
  101.     ns.Width  = nw.Width  = 640;
  102.     ns.Height = nw.Height = 400;
  103.     if (nw.Screen = OpenScreen (&ns)) {
  104.       if (OpenWindow (&nw)) return (nw.Screen);
  105.       else CloseScreen (nw.Screen);
  106.     } else return (NULL);
  107.   }
  108. }
  109.  
  110. /* Macros to extract the color components from a 12-bit RGB color value */
  111. #define RED(c)   ((c & 0x0F00) >> 8)
  112. #define GREEN(c) ((c & 0x00F0) >> 4)
  113. #define BLUE(c)  (c & 0x000F)
  114.  
  115. /* Follows the 11% Blue, 30% Red, 59% Green rule, returns a number 0..15 */
  116. UWORD RGBtoGray (c)
  117. UWORD c;
  118. {
  119.   return (((RED(c) * 77) + (GREEN(c) * 151) + (BLUE(c) * 28)) >> 4);
  120. }
  121.  
  122.  
  123. FindColors (cmap, numcolors)
  124. UWORD *cmap;
  125. int numcolors;
  126. {
  127.   int fdiff = -1, cdiff = 256, ndiff = -1; /* Differences for various colors */
  128.   int cnt, cur, color0;
  129.  
  130.   textcolor = 0;
  131.   bordercolor = 1;
  132.   normcolor = -1;
  133.   if (numcolors > 2) {
  134.     color0 = RGBtoGray (cmap[0]);
  135.     for (cnt = 1; cnt < numcolors; cnt++) {
  136.       cur = RGBtoGray (cmap[cnt]) - color0;
  137.       cur = (cur < 0 ? -cur : cur);
  138.       if (cur > fdiff) {fdiff = cur; textcolor = cnt;};
  139.       if (cur > ndiff && cur < 150) {ndiff = cur; normcolor = cnt;};
  140.       if (cur < cdiff && cur > color0+15) {cdiff = cur; bordercolor = cnt;};
  141.     }  
  142.   }
  143.   if (normcolor == -1) normcolor = textcolor;
  144.   else textcolor = normcolor;
  145.   if (textcolor == bordercolor) textcolor = 0;
  146.   if (normcolor == bordercolor) normcolor = 0;
  147.   if (textcolor == 0) nonzerocolor = bordercolor; else nonzerocolor = textcolor;
  148. }
  149.  
  150. extern struct WBStartup *WBenchMsg;
  151.  
  152. main(argc,argv)
  153. int  argc;
  154. char **argv;
  155. {
  156.   int scrw, scrh, scrd, scrc, cnt;
  157.  
  158.   runningfromcli = (argc == 0 ? false : true);
  159.  
  160.   if ((!(IntuitionBase = OpenLibrary ("intuition.library", 0L))) ||
  161.       (!(GfxBase = OpenLibrary ("graphics.library", 0L))) ||
  162.       (!(LayersBase = OpenLibrary ("layers.library", 0L)))) Panic ("No libs!");
  163.  
  164.   InitRnd ();  /* Initialize the random number generator */
  165.  
  166.   if ((fp = ParseArgs (argc, argv, WBenchMsg)) == NULL)
  167.     Panic ("Could not open file");
  168.  
  169.   if (ReadILBM (fp, &pic) == false) {
  170.     Close (fp); 
  171.     Panic ("File error - Maybe not IFF?");
  172.   };
  173.   Close (fp); 
  174.  
  175.   scrw = pic.bmhd.w;         /* Screen width in BITS */
  176.   scrh = pic.bmhd.h;         /* Screen height in SCANLINES */
  177.   scrd = pic.bmhd.nPlanes;   /* Screen depth in BIT PLANES */
  178.   scrc = pic.colorcount;     /* Screen colors in # of REGISTERS */
  179.  
  180. /*printf ("Scrw %d Scrh %d Scrd %d Scrc %d\n", scrw, scrh, scrd, scrc);*/
  181.  
  182. /*if ((scrw > 352) || (scrh > 235)) Panic ("Need a LO-RES picture");*/
  183.  
  184.   if (scrd > 4) Panic ("Ack! No more than 16 colors!");
  185.   if (scrw < 64 || scrh < 64) Panic ("Picture too small");
  186.  
  187.   /* Create a bitmap, expand the picture, and get rid of the compressed pic.
  188.   */  
  189.  
  190.   if (InitBM  (&picbm, scrh, scrw, scrd) == false) Panic ("No memory for picture");
  191.  
  192.   Expand  (&picbm, &(pic.bmhd), pic.rawsource);
  193.  
  194.   FreeMem (pic.rawsource, pic.rawsourcesize);
  195.   pic.rawsource = NULL;
  196.  
  197.   FindColors (&(pic.colortable), 1 << scrd);
  198.  
  199.   if ((scr0 = OpenPictureScreen (&pic, bordercolor, normcolor)) == NULL)
  200.     Panic ("No memory for screen");
  201.   LoadRGB4 (&(scr0->ViewPort), &(pic.colortable), (LONG)scrc);
  202.  
  203.   /* For now, FORCE some values... */
  204.   puzdepth = scrd;
  205.   picx     = 8;
  206.   picy     = 16;
  207.   pich     = Min (scrh, scr0->Height-picy-180);
  208.   picw     = Min (scrw, scr0->Width-picx*2);
  209.  
  210. /* A word on colors: Globally,
  211. **  normcolor is used for text (different than background, but not much)
  212. **  textcolor is used for bright stuff (highlights)
  213. **  bordercolor is slightly different than background
  214. */
  215.  
  216.   if (InitPzMenu (scrd, normcolor, normcolor, bordercolor) == false) 
  217.     Panic ("No memory for menu");
  218.  
  219.   GetAndHandleEvents (scr0->FirstWindow);
  220.  
  221.   CleanUp ();
  222.  
  223. }
  224.  
  225.